<template>
  <div>
    <n-card title="发布需求">
      <n-form :model="formValue" :rules="rules" ref="formRef" label-placement="left" label-width="120">
        <n-form-item label="需求标题" path="title">
          <n-input v-model:value="formValue.title" placeholder="请输入需求标题" />
        </n-form-item>

        <n-form-item label="需求方名称" path="demanderName">
          <n-input v-model:value="formValue.demanderName" placeholder="请输入需求方名称" />
        </n-form-item>

        <n-form-item label="联系电话" path="contactPhone">
          <n-input v-model:value="formValue.contactPhone" placeholder="请输入联系电话" />
        </n-form-item>

        <n-form-item label="服务地址" path="address">
          <n-input v-model:value="formValue.address" placeholder="请输入服务地址" />
        </n-form-item>

        <n-form-item label="所需技能">
          <n-input v-model:value="formValue.requiredSkills" placeholder="请输入所需技能" />
        </n-form-item>

        <n-form-item label="期望时间" path="expectedTime">
          <n-date-picker v-model:value="formValue.expectedTime" type="datetime" style="width: 100%" />
        </n-form-item>

        <n-form-item label="积分奖励" path="pointsReward">
          <n-input-number v-model:value="formValue.pointsReward" :min="1" style="width: 100%" />
        </n-form-item>

        <n-form-item label="需求描述" path="description">
          <n-input
            v-model:value="formValue.description"
            type="textarea"
            placeholder="请详细描述服务需求"
            :rows="5"
          />
        </n-form-item>

        <n-form-item>
          <n-space>
            <n-button type="primary" @click="handleSubmit">发布需求</n-button>
            <n-button @click="router.back()">取消</n-button>
          </n-space>
        </n-form-item>
      </n-form>
    </n-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { createDemand } from '@/api/demand'

const router = useRouter()
const userStore = useUserStore()
const formRef = ref(null)

const formValue = reactive({
  title: '',
  demanderName: '',
  contactPhone: '',
  address: '',
  requiredSkills: '',
  expectedTime: null,
  pointsReward: 10,
  description: ''
})

const rules = {
  title: [{ required: true, message: '请输入需求标题', trigger: 'blur' }],
  demanderName: [{ required: true, message: '请输入需求方名称', trigger: 'blur' }],
  contactPhone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
  address: [{ required: true, message: '请输入服务地址', trigger: 'blur' }],
  expectedTime: [{ required: true, type: 'number', message: '请选择期望时间', trigger: 'change' }],
  pointsReward: [{ required: true, type: 'number', message: '请输入积分奖励', trigger: 'blur' }],
  description: [{ required: true, message: '请输入需求描述', trigger: 'blur' }]
}

const handleSubmit = () => {
  formRef.value?.validate(async (errors) => {
    if (errors) return

    try {
      const data = {
        ...formValue,
        publisherId: userStore.userId,
        expectedTime: new Date(formValue.expectedTime).toISOString()
      }
      await createDemand(data)
      window.$message?.success('发布成功')
      router.push('/demand/list')
    } catch (error) {
      console.error('发布需求失败:', error)
    }
  })
}
</script>

